﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.ModFlow.Domain.FileLayer
{
    public  class PLayerPropertyFlow:Package
    {
        public PLayerPropertyFlow(int layerNum)
        {
            this._fileType = FileType.LPF;
            this.Fname = "solution.lpf";

            this.Nunit=21;
            this._layerTypes = new int[layerNum];
            this._LAYAVG=new int[layerNum];
            this._CHANI=new double[layerNum];
            this._LAYVKA=new int[layerNum];
            this._LAYWET = new int[layerNum];
            this._LAYCBD = new int[layerNum];
        }


        int _ILPFCB=108;

        /// <summary>
        /// cellbycell写入标志
        /// </summary>
        public int ILPFCB
        {
            get { return _ILPFCB; }
            set { _ILPFCB = value; }
        }


        double _HDRY;

        /// <summary>
        /// 干单元的水头值
        /// </summary>
        public double HDRY
        {
            get { return _HDRY; }
            set { _HDRY = value; }
        }



        int _NPLPF;

        /// <summary>
        /// lpf参数的数量
        /// </summary>
        public int NPLPF
        {
            get { return _NPLPF; }
            set { _NPLPF = value; }
        }

        string _OPTIONS="";

        /// <summary>
        /// 相关选项
        /// STORAGECOEFFICIENT，表明变量Ss和SS读取为储水系数而不是比容量。
        ///CONSTANTCV，表明对没定义的单元垂直传导率来自于单元厚度而不是饱水厚度
        ///THICKSTRT，indicates that layers having a negative LAYTYP are confined, and their cell thickness for conductance calculations will be computed as STRT-BOT rather than TOP-BOT.
        ///NOCVCORRECTION，表明当垂向流动使用时不对垂向传导率就行调整。
        /// </summary>
        public string OPTIONS
        {
            get { return _OPTIONS; }
            set { _OPTIONS = value; }
        }


        int[] _layerTypes;

        public int[] LayerTypes
        {
            get { return _layerTypes; }
            set { _layerTypes = value; }
        }

        int[] _LAYAVG;

        public int[] LAYAVG
        {
            get { return _LAYAVG; }
            set { _LAYAVG = value; }
        }

        double[] _CHANI;

        public double[] CHANI
        {
            get { return _CHANI; }
            set { _CHANI = value; }
        }


        int[] _LAYVKA;


        /// <summary>
        /// 逐层标志位
        /// </summary>
        public int[] LAYVKA
        {
            get { return _LAYVKA; }
            set { _LAYVKA = value; }
        }


        int[] _LAYWET;

        /// <summary>
        /// 逐层湿化标志
        /// </summary>
        public int[] LAYWET
        {
            get { return _LAYWET; }
            set { _LAYWET = value; }
        }


        double _WETFCT;

        /// <summary>
        /// 湿化因子
        /// </summary>
        public double WETFCT
        {
            get { return _WETFCT; }
            set { _WETFCT = value; }
        }

        int _IWETIT;


        /// <summary>
        /// 尝试润湿单元的迭代间隔
        /// </summary>
        public int IWETIT
        {
            get { return _IWETIT; }
            set { _IWETIT = value; }
        }


        int _IHDWET;

        /// <summary>
        /// 
        /// 湿化单元的初始水头计算公式
        /// </summary>
        public int IHDWET
        {
            get { return _IHDWET; }
            set { _IHDWET = value; }
        }



        //下面这个暂时不写

        //ParaObject[] _PARAMS;

        ///// <summary>
        ///// 这里的相关参数
        ///// </summary>
        //public ParaObject[] PARAMS
        //{
        //    get { return _PARAMS; }
        //    set { _PARAMS = value; }
        //}

       
        //item9暂时先不写



        //下面进入逐层二维部分

        List<double[,]> _HK=new List<double[,]>();

        /// <summary>
        /// HK水传到系数，注意这里第一维定义的是ncol,之后是nrow ，列和行数据有可能反向
        /// </summary>
        public List<double[,]> HK
        {
            get { return _HK; }
            
        }


        List<double[,]> _HANI=new List<double[,]> ();

        public List<double[,]> HANI
        {
            get { return _HANI; }
            
        }


        List<double[,]> _VKA=new List<double[,]> ();

        /// <summary>
        /// 垂直水导系数，或者是水平水导系数到垂直系数的比例。（LAYVKA=0）
        /// </summary>
        public List<double[,]> VKA
        {
            get { return _VKA; }
            
        }


        List<double[,]> _Ss=new List<double[,]> ();

        /// <summary>
        /// 存储系数
        /// </summary>
        public List<double[,]> Ss
        {
            get { return _Ss; }
            
        }


        List<double[,]> _Sy = new List<double[,]>();

        public List<double[,]> Sy
        {
            get { return _Sy; }
            
        }

        List<double[,]> _VKCB = new List<double[,]>();

        public List<double[,]> VKCB
        {
            get { return _VKCB; }

        }


        List<double[,]> _WETDRY = new List<double[,]>();

        /// <summary>
        /// 干湿转化标志，只在层可转化，且湿化开启的情况下调用
        /// </summary>
        public List<double[,]> WETDRY
        {
            get { return _WETDRY; }

        }

        private bool _IsHaveTransient = false;
        /// <summary>
        /// 是否存在时间离散，时间应力期，默认否
        /// </summary>
        public bool IsHaveTransient
        {
            get { return _IsHaveTransient; }
            set { _IsHaveTransient = value; }
        }

        /// <summary>
        /// 数量等于NLAY，指示该层下方是否含有隔水层，
        /// 0 表示没有，不等于0表示有，最后一层必须写为0
        /// </summary>
        int[] _LAYCBD;

        public int[] LAYCBD
        {
            get { return _LAYCBD; }
            set { _LAYCBD = value; }
        }

        public override void WriteFile(string path)
        {
            string content = "# HuanAnKeJi LPF File" + "\r\n";
            content += this.ILPFCB + "  " + this.HDRY + "  " + this.NPLPF + "          Item 1: ILPFCB HDRY NPLPF" + "\r\n";
            for (int i = 0; i < this.LayerTypes.Length; i++)
            {
                content += this.LayerTypes[i] + "  ";
            }
            content += "              Item 2: LAYTYP" + "\r\n";
            for (int i = 0; i < this.LAYAVG .Length; i++)
            {
                content += this.LAYAVG[i] + "  ";
            }
            content += "              Item 3: LAYAVG" + "\r\n";  //暂时不知是什么意思

            for (int i = 0; i < this.CHANI .Length; i++)
            {
                content += this.CHANI[i] + "  ";
            }
            content += "              Item 4: CHANI" + "\r\n";

            for (int i = 0; i < this.LAYVKA  .Length; i++)
            {
                content += this.LAYVKA[i] + "  ";
            }
            content += "              Item 5: LAYVKA" + "\r\n";

            bool P = false; //是否湿化标记，默认为不湿化
            for (int i = 0; i < this.LAYWET.Length; i++)
            {
                content += this.LAYWET[i] + "  ";
                if (this.LAYWET[i] > 0)
                {
                    P = true;
                }
            }
            content += "              Item 6: LAYWET" + "\r\n";
            if (P)//如果湿化则写Item 7 Layer
            {
                content += this.WETFCT + "  " + this.IWETIT + "  " + this.IHDWET + "          Item 7: WETFCT IWETIT IHDWET  \r\n";
            }
                for (int i = 0; i < this.HK.Count; i++)
            {

                if (IsConstant(this.HK[i]))
                {
                    content += "CONSTANT " + string.Format("{0,10}", this.HK[i][0, 0]) + "        Item 10: HK layer " + (i + 1).ToString() + "\r\n";
                }
                else
                {
                    content += "INTERNAL   1.0 (FREE)  19" + "          Item 10: HK layer " + (i + 1).ToString() + "\r\n";
                    string str = "";
                    if (GetArrayString(this.HK[i], ref str))
                    {
                        content += str;
                    }
                }

                if (this.CHANI[i] <= 0)
                {
                    if (IsConstant(this.HANI[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.HANI[i][0, 0]) + "          Item 11: HANI layer  " + (i + 1).ToString() + "\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "          Item 11: HANI layer  " + (i + 1).ToString() + "\r\n";
                        string str = "";
                        if (GetArrayString(this.HANI[i], ref str))
                        {
                            content += str;
                        }
                    }
                }

                if (IsConstant(this.VKA[i]))
                {
                    content += "CONSTANT " + string.Format("{0,10}", this.VKA[i][0, 0]) + "          Item 12: VKA layer  " + (i + 1).ToString() + "\r\n";
                }
                else
                {
                    content += "INTERNAL   1.0 (FREE)  19" + "           Item 12: VKA, layer " + (i + 1).ToString() + "\r\n";
                    string str = "";
                    if (GetArrayString(this.VKA[i], ref str))
                    {
                        content += str;
                    }
                }

                if (this.OPTIONS != null)
                {
                    if (this.IsHaveTransient) //this.OPTIONS.Contains("STORAGECOEFFICIENT") &&   写DOS的标志位不作判断 
                    {
                        if (IsConstant(this.Ss[i]))
                        {
                            content += "CONSTANT " + string.Format("{0,10}", this.Ss[i][0, 0]) + "            Item 13: SS layer  " + (i + 1).ToString() + "\r\n";
                        }
                        else
                        {
                            content += "INTERNAL   1.0 (FREE)  19" + "           Item 13: SS, layer " + (i + 1).ToString() + "\r\n";
                            string str = "";
                            if (GetArrayString(this.Ss[i], ref str))
                            {
                                content += str;
                            }
                        }
                    }
                }

                if (this.IsHaveTransient)
                {
                    if (this.LayerTypes[i] != 0)
                    {
                        if (IsConstant(this.Sy[i]))
                        {
                            content += "CONSTANT " + string.Format("{0,10}", this.Sy[i][0, 0]) + "           Item 14: SY layer  " + (i + 1).ToString() + "\r\n";
                        }
                        else
                        {
                            content += "INTERNAL   1.0 (FREE)  19" + "           Item 14: SY, layer " + (i + 1).ToString() + "\r\n";
                            string str = "";
                            if (GetArrayString(this.Sy[i], ref str))
                            {
                                content += str;
                            }
                        }
                    }
                }




                    ///只有在是承压层，并且不是最后一层时，写此参数
                if (this.LAYCBD[i]!=0&&i!=this.HK.Count-1)
                {


                    if (IsConstant(this.VKCB[i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.VKCB[i][0, 0]) + "            Item 15: VKCB layer  " + (i + 1).ToString() + "\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "           Item 15: VKCB, layer " + (i + 1).ToString() + "\r\n";
                        string str = "";
                        if (GetArrayString(this.VKCB[i], ref str))
                        {
                            content += str;
                        }
                    }

                }

                if (this.LayerTypes[i] != 0 && this.LAYWET[i] != 0)
                {
                    if (IsConstant(this.WETDRY [i]))
                    {
                        content += "CONSTANT " + string.Format("{0,10}", this.WETDRY[i][0, 0]) + "           Item 16: WETDRY layer  " + (i + 1).ToString() + "\r\n";
                    }
                    else
                    {
                        content += "INTERNAL   1.0 (FREE)  19" + "           Item 16: WETDRY, layer " + (i + 1).ToString() + "\r\n";
                        string str = "";
                        if (GetArrayString(this.WETDRY[i], ref str))
                        {
                            content += str;
                        }
                    }
                }

            }
            

            string fname = path + this.Fname;
            HAUtil.FileOperations.WriteFile(fname, content);
        }

    }
}
